package com.zbkj.crmeb.payment.controller;

import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.utils.adapay.degree.ServletUtils;
import com.zbkj.crmeb.payment.service.CallbackService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


/**
 * 支付回调
 */
@RestController
@RequestMapping("api/admin/payment/callback")
@Api(tags = "支付回调")

public class CallbackController {

    private static Logger logger = LoggerFactory.getLogger(CallbackController.class);

    @Autowired
    private CallbackService callbackService;
    private static final String RESP_MSG ="SUCCESS";

    /**
     * 微信支付回调
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "微信支付回调")
    @RequestMapping(value = "/wechat", method = RequestMethod.POST)
    public void weChat(@RequestBody String  request, HttpServletResponse response) {
        logger.info("微信支付回调");
        //微信支付回调
        try{
            if(callbackService.weChat(request)){
                response.getWriter().write("<xml>\n" +
                        "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                        "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                        "</xml>");

            }else{
                response.getWriter().write("<xml>\n" +
                        "  <return_code><![CDATA[FAIL]]></return_code>\n" +
                        "  <return_msg><![CDATA[pay fail]]></return_msg>\n" +
                        "</xml>");
            }
        }catch (Exception e){
            logger.error("微信回调失败:{},request:{}",e,request);
        }

    }

    /**
     * 支付宝支付回调
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "支付宝支付回调 ")
    @RequestMapping(value = "/alipay", method = RequestMethod.POST)
    public void aliPay(@RequestBody String request){
        //支付宝支付回调
        callbackService.aliPay(request);
    }

    /**
     * 橙子支付回调
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "橙子支付回调")
    @RequestMapping(value = "/notify", method = RequestMethod.POST)
    public void orangePay(HttpServletResponse response, HttpServletRequest httpServletRequest){
        Map<String, String[]> map = httpServletRequest.getParameterMap();
        logger.info("橙子支付回调请求,Map:{}", JSONObject.toJSONString(map));
        Boolean pay = callbackService.orangePay(map);
        
        try {
            if(pay){
                response.getWriter().write("{\"code\":\"SUCCESS\",\"success\":1}");
                 }else {
                response.getWriter().write("{\"code\":\"error\",\"success\":0}");
              }
        } catch (IOException e) {
            logger.error("橙子回调失败,e",e);
        }
    }

    /**
     * 橙子充值支付回调
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "橙子充值回调")
    @RequestMapping(value = "/rechargeNotify", method = RequestMethod.POST)
    public void rechargeNotify(HttpServletResponse response, HttpServletRequest httpServletRequest){

        Map map = httpServletRequest.getParameterMap();
        logger.info("橙子充值回调请求,Map:{}", JSONObject.toJSONString(map));
        Boolean pay = callbackService.rechargeNotify(map);
        try {
            if(pay){
                response.getWriter().write("{\"code\":\"SUCCESS\",\"success\":1}");
            }else {
                response.getWriter().write("{\"code\":\"error\",\"success\":0}");
            }
        } catch (IOException e) {
            logger.error("橙子充值回调失败,e",e);
        }
    }

    /**
     * 橙子购买会员回调
     */
    @ApiOperation(value = "橙子支付回调")
    @RequestMapping(value = "/member/notify/orange", method = RequestMethod.POST)
    public void notifyMemberOrange(HttpServletResponse response, HttpServletRequest httpServletRequest){
        Map<String, String[]> map = httpServletRequest.getParameterMap();
        logger.info("橙子支付回调请求,Map:{}", JSONObject.toJSONString(map));
        Boolean pay = callbackService.notifyMemberOrange(map);

        try {
            if(pay){
                response.getWriter().write("{\"code\":\"SUCCESS\",\"success\":1}");
            }else {
                response.getWriter().write("{\"code\":\"error\",\"success\":0}");
            }
        } catch (IOException e) {
            logger.error("橙子回调失败,e",e);
        }
    }

    /**
     * 香港购买会员回调
     */
    @ApiOperation(value = "香港支付支付回调")
    @RequestMapping(value = "//member/notify/hk", method = RequestMethod.POST)
    public void notifyMemberHk(@RequestBody String  request, HttpServletResponse response) throws IOException {
        logger.info("香港支付支付回调请求,request:{}", request);
        //微信支付回调
        try{
            if(callbackService.notifyMemberHk(request)){
                response.getWriter().write("success");
            }else{
                response.getWriter().write("fail");
            }
        }catch (Exception e){
            logger.error("香港支付回调失败:{},request:{}",e,request);
            response.getWriter().write("fail");
        }
    }

    /**
     * 香港支付支付回调
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "香港支付支付回调")
    @RequestMapping(value = "/notifyHK", method = RequestMethod.POST)
    public void HKPay(@RequestBody String  request, HttpServletResponse response) throws IOException {
        logger.info("香港支付支付回调请求,request:{}", request);
        //微信支付回调
        try{
            if(callbackService.HKPay(request)){
                response.getWriter().write("success");
            }else{
                response.getWriter().write("fail");
            }
        }catch (Exception e){
            logger.error("香港支付回调失败:{},request:{}",e,request);
            response.getWriter().write("fail");
        }
    }

    /**
     * 连连聚合支付回调
     * @author daigb
     * @date:  2022-07-20 14:12
     */
    @ApiOperation(value = "连连聚合支付支付回调")
    @RequestMapping(value = "/notifyLianlianDegree", method = RequestMethod.POST)
    public String notifyLianlianDegree(HttpServletRequest request) {

        try {
            if(callbackService.lianlianDegreePay(request)){
                // 返回Success，响应本次异步通知已经成功
                return "Success";
            }
        } catch (Exception e){
            logger.error("连连聚合支付支付回调失败,", e);
        }

        // 没有其他意义，异步通知响应连连这边只认"Success"，返回非"Success"，连连会进行重发
        return "error";
    }

    /**
     * 连连聚合支付回调
     * @author daigb
     * @date:  2022-07-20 14:12
     */
    @ApiOperation(value = "连连聚合支付退款回调")
    @RequestMapping(value = "/notifyLianlianDegree/refund", method = RequestMethod.POST)
    public String notifyLianlianDegreeRefund(HttpServletRequest request) {
        try {
            if(callbackService.lianlianDegreeRefund(request)){
                // 返回Success，响应本次异步通知已经成功
                return "Success";
            }
        } catch (Exception e){
            logger.error("连连聚合支付支付回调失败:", e);
        }

        // 没有其他意义，异步通知响应连连这边只认"Success"，返回非"Success"，连连会进行重发
        return "error";
    }

    /**
     * 连连聚合支付海关推单回调
     * @author daigb
     * @date:  2022-07-20 14:12
     */
    @ApiOperation(value = "连连聚合支付海关推单回调")
    @RequestMapping(value = "/notifyLianlianDegree/customs", method = RequestMethod.POST)
    public String notifyLianlianDegreeCustoms(HttpServletRequest request) {
        try {
            if(callbackService.lianlianDegreeCustoms(request)){
                // 返回Success，响应本次异步通知已经成功
                return "Success";
            }
        } catch (Exception e){
            logger.error("连连聚合支付海关推单回调失败:", e);
        }

        // 没有其他意义，异步通知响应连连这边只认"Success"，返回非"Success"，连连会进行重发
        return "error";
    }

    /**
     * 汇聚聚合支付回调(包含支付, 退款, 充值, 会员购买, 报关)
     * @author daigb
     * @date:  2022-07-20 14:12
     */
    @ApiOperation(value = "汇付聚合支付支付回调")
    @RequestMapping(value = "/notifyAdapayDegree", method = RequestMethod.POST)
    public void notifyAdapayDegree(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // TODO DIDIOK 支付回调21
        Map<String, Object> parameter = ServletUtils.getRequestParameters(request);
    	logger.info("汇付聚合支付支付回调, 参数:{}", JSON.toJSONString(parameter));
    	try {
    		if(callbackService.adapayDegreePay(parameter)){
    			response.getWriter().write(RESP_MSG);
    			return;
    		}
    	} catch (Exception e){
    		logger.error("汇付聚合支付支付回调失败:{}, 参数:{}", e, JSON.toJSONString(parameter));
    	}
    	
    	response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
    
    /**
     * 汇聚国内支付回调(包含支付, 退款, 充值, 会员购买)
     * @author daigb
     * @date:  2022-07-20 14:12
     */
    @ApiOperation(value = "汇付国内支付支付回调")
    @RequestMapping(value = "/notifyAdapayCn", method = RequestMethod.POST)
    public void notifyCnDegree(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // TODO DIDIOK 支付回调11
        Map<String, Object> parameter = ServletUtils.getRequestParameters(request);
    	logger.info("汇付国内支付支付回调, 参数:{}", JSON.toJSONString(parameter));
    	try {
    		if(callbackService.adapayCnPay(parameter)){
    			response.getWriter().write(RESP_MSG);
    			return;
    		}
    	} catch (Exception e){
    		logger.error("汇付国内支付支付回调失败:{}, 参数:{}", e, JSON.toJSONString(parameter));
    	}
    	
    	response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }

    /**
     * 橙子充值支付回调
     * @author Mr.Zhang
     * @since 2020-05-06
     */
    @ApiOperation(value = "香港支付充值回调")
    @RequestMapping(value = "/rechargeNotifyHK", method = RequestMethod.POST)
    public void rechargeNotifyHK(@RequestBody String  request, HttpServletResponse response) throws IOException {
        logger.info("香港支付充值回调请求,request:{}", request);
        try {
            if(callbackService.rechargeNotifyHK(request)){
                response.getWriter().write("success");
            }else{
                response.getWriter().write("fail");
            }
        } catch (IOException e) {
            logger.error("香港支付充值回调失败,e",e);
            response.getWriter().write("fail");
        }
    }

}



